/**
 * 
 */
package com.xintoucloud.gsv.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.gsv.entity.Role;
import com.xintoucloud.gsv.entity.RolePermission;
import com.xintoucloud.gsv.entity.RoleState;
import com.xintoucloud.gsv.model.RoleVo;
import com.xintoucloud.gsv.service.RolePermissionService;
import com.xintoucloud.gsv.service.RoleService;

/**
 * @author xiang
 *
 */
@Controller@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private RolePermissionService rolePermissionService;
	
	@RequestMapping("/index")   //连接先进到控制器, 返回值在进入要显示数据的html页面NewsManagement2
	public String index(Model m) {
		List<RolePermission> rolePermissions = rolePermissionService.list();
		m.addAttribute("rolePermissions", rolePermissions);
 		return "roleManagement";
	}
	
	@RequestMapping("/list")
	@ResponseBody			//把数据封装到map中,返回的map会被自动解析	
	public Map<String ,Object> roleList() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<RoleVo> roleVos = roleService.showView();
		map.put("code", 0);   //1,code:解析接口状态
		map.put("data",roleVos); //2,msg:解析提示文本
		return map;			  //3,count:解析数据长度
							  //4,data:解析数据列表
	}
	
	@RequestMapping("/roleAdd")
	public String addUser(Role role,int rpid, int rsid) {
		RolePermission rp = new RolePermission();
		rp.setRpid(rpid);
		RoleState rs = new RoleState();
		rs.setRsid(rsid);
		role.setRolePermission(rp);
		role.setRoleState(rs);
		roleService.roleAdd(role);
		return "redirect:index";
	}

	
	@RequestMapping("/roleEdit")
	public String roleEdit(Role role,int rpid,int rsid) {
		RolePermission rp = new RolePermission();
		rp.setRpid(rpid);
		RoleState rs = new RoleState();
		rs.setRsid(rsid);
		role.setRolePermission(rp);
		role.setRoleState(rs);
		roleService.roleUpdate(role);
		return "redirect:index";
	}

	
	@RequestMapping("/roleDelete")
	@ResponseBody
	public Map<String ,Object> roleDelete(int rid) {
		Map<String,Object> map = new HashMap<String,Object>();
		boolean result = roleService.roleDelete(rid);
		if(result) {
			map.put("msg", "恭喜您,删除成功!");
		}else {
			map.put("msg", "很遗憾,删除失败!");
		}
		return map;
	}

}
